﻿
namespace HSS.ComponentModel
{
	#region Using Directives
	using System;
	using HSS.Resources;

	#endregion

	#region EditorAttribute
	/// <summary>
	/// Specifies the editor to use to change a property. This class cannot be inherited.
	/// </summary>
	[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1019:DefineAccessorsForAttributeArguments"), AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = true)]
	public sealed class EditorAttribute : Attribute
	{
		private string typeName;

		/// <summary>
		/// Initializes a new instance of the EditorAttribute class with the default editor, which is no editor.
		/// </summary>
		public EditorAttribute()
		{
			this.typeName = string.Empty;
		}
		/// <summary>
		/// Initializes a new instance of the EditorAttribute class with the type name of the editor.
		/// </summary>
		/// <param name="typeName">The fully qualified type name of the editor.</param>
		public EditorAttribute(string typeName)
		{
			Guard.ArgumentNotNullOrEmptyOrWhitespace(typeName, "typeName");
			this.typeName = typeName;
		}
		/// <summary>
		/// Initializes a new instance of the EditorAttribute class with the type.
		/// </summary>
		/// <param name="type">A Type that represents the type of the editor.</param>
		public EditorAttribute(Type type)
		{
			Guard.ArgumentNotNull(type, "type");
			this.typeName = type.AssemblyQualifiedName;
		}

		/// <summary>
		/// Gets the name of the editor class in the Type.AssemblyQualifiedName format.
		/// </summary>
		public string EditorTypeName
		{
			get { return this.typeName; }
		}

		/// <summary>
		/// This member overrides Attribute.Equals(Object).
		/// </summary>
		/// <param name="obj">The object to test the value equality of.</param>
		/// <returns>true if the value of the given object is equal to that of the current object; otherwise, false.</returns>
		public override bool Equals(object obj)
		{
			if (obj == this)
				return true;
			var attribute = obj as EditorAttribute;
			return ((attribute != null) && (attribute.typeName == this.typeName));
		}
		/// <summary>
		/// This member overrides Attribute.GetHashCode().
		/// </summary>
		/// <returns>A 32-bit signed integer hash code.</returns>
		public override int GetHashCode()
		{
			return base.GetHashCode();
		}
	}
	#endregion

	internal enum BuiltInEditors
	{
		DateTimeValueEditor,
		BooleanValueEditor,
		EnumValueEditor,
		EnumFlagValueEditor,
		StringValueEditor,
		ColorValueEditor,
		CollectionValueEditor,
		ObjectValueEditor,
		ThicknessValueEditor
	}

	internal static class BuiltInEditor
	{
		private static string AsmTemplate
		{
			get
			{
				if (string.IsNullOrEmpty(_asmTemplate))
					_asmTemplate = ResourceProvider.GetString(StringKeys.AsmTemplate);
				return _asmTemplate;
			}
		} static string _asmTemplate;

		public static string GetAssemblyQualifiedName(BuiltInEditors editor)
		{
			return string.Format(System.Globalization.CultureInfo.InvariantCulture, AsmTemplate, GetFullName(editor));
		}
		public static string GetFullName(BuiltInEditors editor)
		{
			var template = ResourceProvider.GetString(StringKeys.BuiltInControlTemplate);
			return string.Format(System.Globalization.CultureInfo.InvariantCulture, template, editor.ToString());
		}
	}
}